package com.example.module_hp_zao_yin.fragment;


import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.module_hp_zao_yin.BR;

import com.example.module_hp_zao_yin.R;
import com.example.module_hp_zao_yin.adapter.HpZaoYinListAdapter;
import com.example.module_hp_zao_yin.base.BaseMvvmFragment;
import com.example.module_hp_zao_yin.base.BaseViewModel;
import com.example.module_hp_zao_yin.databinding.FragmentHpZaoYinListMainBinding;
import com.example.module_hp_zao_yin.entity.HpAudioEntity;
import com.example.module_hp_zao_yin.utils.HpFileUtil;
import com.fwlst.lib_base.utils.AdUtils;
import com.fwlst.lib_base.utils.BaseUtils;
import com.fwlst.lib_base.utils.MmkvUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;

@Route(path = "/hpZaoYin/route/HpZaoYinListMainFragment")
public class HpZaoYinListMainFragment extends BaseMvvmFragment<FragmentHpZaoYinListMainBinding, BaseViewModel> {
    private HpZaoYinListAdapter hpZaoYinListAdapter;
    private List<HpAudioEntity> mDataList;

    @Override
    protected int initLayout() {
        return R.layout.fragment_hp_zao_yin_list_main;
    }

    @Override
    protected int initBR() {
        return BR.data;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        //初始化列表
        hpZaoYinListAdapter = new HpZaoYinListAdapter();
        binding.hpZaoYinListRv.setLayoutManager(new LinearLayoutManager(mContext));
        binding.hpZaoYinListRv.setAdapter(hpZaoYinListAdapter);
        hpZaoYinListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                AdUtils.INSTANCE.checkFunction(requireContext(), new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        ARouter.getInstance()
                                .build("/hpVideoRing/route/HpAudioEditMainActivity")
                                .withString("audioFilePath", mDataList.get(position).getPath()).navigation();
                        return null;
                    }
                });
            }
        });
        //设置空视图
        View emptyView = getLayoutInflater().inflate(R.layout.base_item_empty, null);
        hpZaoYinListAdapter.setEmptyView(emptyView);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            Log.e("显示状态:", String.valueOf(hidden));
            getDataList();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //获取列表数据
        getDataList();
    }

    //根据file地址列表获取audio列表
    private void getDataList() {
        //获取列表数据
        String listStr = MmkvUtils.INSTANCE.get("mAudioList", "");
        Type typeToken = new TypeToken<List<HpAudioEntity>>() {
        }.getType();
        //将list集合反序列化
        mDataList = new Gson().fromJson(listStr, typeToken);
        if (mDataList == null) {
            mDataList = new ArrayList<>();
        }
        for (HpAudioEntity item : mDataList) {
            File file = new File(item.getPath());
            if (!file.exists()) {
                continue;
            }
            item.setDate(BaseUtils.INSTANCE.formatTime((int) (file.lastModified() / 1000), "yyyy-MM-dd HH:mm"));//日期
            //设置文件时长
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            retriever.setDataSource(item.getPath());
            //得到的数除1000得到秒，再把秒分为时分秒
            int time = Integer.parseInt(retriever.extractMetadata
                    (MediaMetadataRetriever.METADATA_KEY_DURATION)) / 1000;
            item.setTime(HpFileUtil.formatTime(time));//时长
        }
        hpZaoYinListAdapter.setNewData(mDataList);
    }
}